package net.siufung.core.utils;

import cn.hutool.core.date.DatePattern;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Gson工具类
 * @author 陈建峰
 * @since 2022/4/26 5:28 下午
 */
@NoArgsConstructor
public class GsonUtil {

    private static final Gson GSON = new GsonBuilder().setDateFormat(DatePattern.NORM_DATETIME_PATTERN).create();;

    /**
     * 判断gsonString是否是json格式
     * @param gsonString gsonString字符串
     * @return T
     */
    public static boolean isJson(String gsonString) {
        if(!Objects.isNull(gsonToBean(gsonString, Object.class))){
            return Boolean.TRUE;
        };
        return Boolean.FALSE;
    }

    /**
     * 将对象转成json字符串格式
     * @param object 对象
     * @return json字符串
     */
    public static String toGsonString(Object object) {
        String gsonString = StringPool.EMPTY;
        try {
            gsonString = GSON.toJson(object);
        } catch (JsonIOException e) {
            e.printStackTrace();
        }
        return gsonString;
    }

    /**
     * 将gsonString转成特定的tClass的对象
     * @param gsonString gsonString字符串
     * @param tClass 转成的tClass的对象
     * @return T
     */
    public static <T> T gsonToBean(String gsonString, Class<T> tClass) {
        try {
            return GSON.fromJson(gsonString, tClass);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将gsonString字符串转成list
     * @param gsonString gsonString字符串
     * @param tClass 转成的tClass的对象
     * @param <T> 实体
     * @return T
     */
    public static <T> List<T> gsonToList(String gsonString, Class<T> tClass) {
        List<T> tList = Lists.newArrayList();
        try {
            tList = GSON.fromJson(gsonString, new TypeToken<List<T>>(){}.getType());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return tList;
    }

    /**
     * jsonArrayString字符串转成list
     * @param gsonArrayString 集合字符串
     * @param tClass 转换的实体类
     * @param <T> 实体
     * @return T
     */
    public static <T> List<T> gsonArrayToList(String gsonArrayString, Class<T> tClass){
        ArrayList<T> mList = Lists.newArrayList();
        try {
            JsonArray array = JsonParser.parseString(gsonArrayString).getAsJsonArray();
            for(final JsonElement elem : array){
                mList.add(GSON.fromJson(elem, tClass));
            }
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return mList;
    }

    /**
     * gson字符串转成map
     * @param gsonString gsonString字符串
     * @param <T> 实体
     * @return Map<String, T> 转换不成功返回 Map<String, T>
     */
    public static <T> Map<String, T> gsonToMap(String gsonString) {
        Map<String, T> map = Maps.newHashMap();
        try {
            map = GSON.fromJson(gsonString, new TypeToken<Map<String, T>>() {}.getType());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * gsonArray字符串转成list中有map
     * @param gsonArrayString gsonString字符串
     * @param <T> T
     * @return List<Map<String, T>> 转换不成功返回 Lists.newArrayList()
     */
    public static <T> List<Map<String, T>> gsonToMapList(String gsonArrayString){
        List<Map<String, T>> list = Lists.newArrayList();
        try {
            list = GSON.fromJson(gsonArrayString,new TypeToken<List<Map<String, T>>>() {}.getType());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * json转换器
     */
    public interface Gsonable extends Serializable {

        /**
         * 转化成Json字符串
         * @return String
         */
        default String toJsonString() {
            return GsonUtil.toGsonString(this);
        }

    }
    

}
